package com.scope.customViews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Checkable;
import android.widget.GridView;

import com.scope.R;

import java.io.File;
import java.util.List;

/**
 * Created by Samrith on 11/1/13.
 */
public abstract class FileExplorer<Adapter extends FileExplorerAdapter> extends GridView implements CustomView.IAccent {
    public FileExplorer(Context context) {
        this(context, null);
    }

    public FileExplorer(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setChoiceMode(CHOICE_MODE_SINGLE);
        setGravity(Gravity.CENTER);

        init(attrs);
    }

    protected Context mContext = null;

    protected abstract Adapter createAdapter();

    private void init(AttributeSet attrs) {
        mAdapter = createAdapter();

        TypedArray arr = mContext.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.LabelButton,
                0, 0
        );

        String path;
        int accent;
        try {
            path = arr.getString(R.styleable.FileExplorer_path);
            accent = arr.getColor(R.styleable.FileExplorer_accent, android.R.color.transparent);
        } finally {
            arr.recycle();
        }

        setAdapter(mAdapter);
        setPath(path);
        setAccent(accent);

        super.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                if (mAdapter.isDirectory(position)) {
                    setItemChecked(position, false);
                    mOnFolderClickListener.onItemClick(parent, v, position, id);
                } else {
                    mOnFileClickListener.onItemClick(parent, v, position - mAdapter.getFolderCount(), id);
                }
            }
        });
    }

    @Override
    public void setOnItemClickListener(OnItemClickListener listener) {
        throw new UnsupportedOperationException();
    }

    private OnItemClickListener mOnFileClickListener;

    public void setOnFileClickLister(OnItemClickListener listener) {
        mOnFileClickListener = listener;
    }

    private OnItemClickListener mOnFolderClickListener;

    public void setOnFolderClickListener(OnItemClickListener listener) {
        mOnFolderClickListener = listener;
    }

    @Override
    public void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
        setNumColumns(width / mContext.getResources().getDimensionPixelSize(R.dimen.button_width_v_m));
    }

    public String getPath() {
        return mAdapter.getRootPath();
    }

    public void setPath(String path) {
        setItemChecked(INVALID_POSITION, true);
        mAdapter.setRootPath(path);
    }

    private Adapter mAdapter = null;

    public int getAccent() {
        return mAdapter.getAccent();
    }

    public void setAccent(int accent) {
        mAdapter.setAccent(accent);
    }

    public List<String> getFilePaths() {
        return mAdapter.getFilePaths();
    }

    public List<String> getFolderPaths() {
        return mAdapter.getFolderPaths();
    }

    public View selectFileAtPosition(int position) {
        if (position == INVALID_POSITION) {
            return null;
        }

        position += mAdapter.getFolderCount();
        smoothScrollToPosition(position);
        setItemChecked(position, true);
        View view = getChildAt(position - getFirstVisiblePosition());
        if (view != null) {
            ((Checkable)view).setChecked(true);
        }
        return view;
    }
}
